<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_scrmtask/model')->方法名();
*/
namespace jy_scrmtask;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function check_custom(){
		if($_SESSION['container']!='wxwork'){
			exi('请在企业微信端环境下打开！','error');
		}
		return true;
	}
	/*
		员工消息推送提醒
	*/
	public function send_task($taskid,$isnoexi=false){
		$task=pdo_get('jy_scrmtask_list',array('id'=>$taskid));
		if(!$task['status']){
			return false;
		}
		if(!$isnoexi && $task['status']==2){
			exi('该任务已推送！可以修复任务状态，重新推送！','error');
		}
		$task['workers']=$task['workers']?json_decode($task['workers'],true):array();
		$task['images']=$task['images']?json_decode($task['images'],true):array();
		$task['tuwen']=$task['tuwen']?json_decode($task['tuwen'],true):array();
		if($task['worker_type']){
			$workers=$task['workers'];
		}else{
			$workergroup=str_array($task['workergroup']);
			if(!$workergroup){
					$workergroup=-1;
			}
			$workers=pdo_getall('jy_scrm_worker',array('main_department'=>$workergroup),array('id','name'),'id');
		}
		foreach($workers as $wv){
			if(!$wv['id']){
				continue;
			}
			$data=array(
				'workerid'=>$wv['id'],
				'tid'=>$taskid,
				'type'=>$task['type'],
				'ctype'=>$task['ctype'],
				'gtype'=>$task['gtype'],
				'usernum'=>$task['usernum'],
				'status'=>0,
			);
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_scrmtask_log',$data);
			$logid=pdo_insertid();
			$title=$this->task_type[$task['type']];
			$description='管理员给你安排了'.$title.'的任务，赶快去获取素材并完成任务吧！';
			$url=custom_url('jy_scrmtask/log_item//id='.$logid,true);
			$content=array(
					'msgtype'=>'textcard',
					'textcard'=>array(
						'title'=>$title.'提醒',
						'description'=>$description,
						'url'=>$url,
						'btntxt'=>'详情',
					),
			);
			$res=$this->onemass($wv['id'],$content,$isnoexi);
		}
		pdo_update('jy_scrmtask_list',array('status'=>$task['tixin_type']?1:2,'send_time'=>TIMESTAMP),array('id'=>$taskid,'status'=>1));
		return true;
	}
	/*
		推送消息
	*/
	public function onemass($workerid,$content=array(),$isnoexi=false){
			if(!$workerid){
				if(!$isnoexi){
					exi('操作失败！员工ID不存在！','error');
				}
				return false;
			}
			$worker=pdo_get('jy_scrm_worker',array('id'=>$workerid),'userid','userid');
			$touser=$worker['userid'];
			if(!$touser){
				if(!$isnoexi){
					exi('操作失败！该员工没有同步企业微信的OPENID！','error');
				}
				return false;
			}
			$content['touser']=$touser;
			model('core/wxwork_userapi')->message($content);
			return true;
	}
	/*
		目标人数
	*/
	public function get_usernum($data,$gtype=0){
			$params=array('uniacid'=>$_SESSION['uniacid']);
			if($data['worker_type']){
				$workers=array();
				foreach($data['workers'] as $wv){
					$workers[]=$wv['id'];
				}
				if(!$workers){
					$workers=0;
				}
				$params['workerid']=$workers;
			}else{
				$workergroup=str_array($data['workergroup']);
				if(!$workergroup){
					$workergroup=-1;
				}
				$workers=pdo_getall('jy_scrm_worker',array('main_department'=>$workergroup),'id','id');
				if($workers){
					$workers=array_keys($workers);
				}else{
					$workers=0;
				}
				$params['workerid']=$workers;
			}
			if($gtype==0 || $gtype==4){
				$params['tags like']='%'.trim($data['tags']).'%';
			}
			$time=$post['tixin_days']*24*3600;
			$day=date('m-d',time()-$time);
			if($gtype==1){
				$params['birth like']='%'.$day.'%';
			}
			//结婚纪念日跟进
			if($gtype==2){
				$params['jiehunday like']='%'.$day.'%';
			}
			//3-3-3回访
			if($gtype==3){
				if($data['gtype3_type']==0){
					$lastbuys=$day3=strtotime(date('Y-m-d',strtotime("-3 day")));
				}
				if($data['gtype3_type']==1){
					$lastbuys=$week3=strtotime(date('Y-m-d',strtotime("-21 day")));
				}
				if($data['gtype3_type']==2){
					$lastbuys=$month3=strtotime(date('Y-m-d',strtotime("-3 month")));
				}
				$params['lastbuy']=$lastbuys;
			}
			//新客户定期跟进
			if($gtype==5){
				$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
				$params['createtime >']=$daytime;
			}
			$usernum=pdo_count('jy_scrm_member',$params);
			return $usernum;
	}
	/*
		计算私域运营任务
	*/
	public function siyu_uplog($log=array(),$usernum=0){
			$workerid=$log['workerid'];
			if($log['status']==2){
				return false;
			}
			if(!$log['usernum']){
				pdo_update('jy_scrmtask_log',array('status'=>2),array('id'=>$log['id']));
				return false;
			}
			if(!$log['gtype']){
				$params=array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$workerid);
				$params['createtime >']=$log['createtime'];
				$addnum=pdo_count('jy_scrm_member',$params);
			}else{
				$addnum=0;
				$owner=model('jy_scrm/worker')->get_userid($workerid);
				$params=array('uniacid'=>$_SESSION['uniacid'],'owner'=>$owner);
				$quns=pdo_getall('jy_scrm_qun',$params);
				foreach($quns as $qv){
					$member_list=json_decode($qv['member_list'],true);
					foreach($member_list as $mv){
						if($mv['join_time']>=$log['createtime'] && $mv['userid']==$owner){
							$addnum +=1;
						}
					}
				}
			}
			if($addnum && $addnum>=$log['usernum']){
				pdo_update('jy_scrmtask_log',array('status'=>2,'isusernum'=>$addnum),array('id'=>$log['id']));
				return false;
			}
			return $addnum;
	}
}
?>